package cn.gd.controller.Document;

import cn.gd.pojo.DocumentTool.DocumentVo;
import cn.gd.pojo.Documentrecyclebin;
import cn.gd.pojo.Documents;
import cn.gd.service.Document.DocumentService;
import cn.gd.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wyh
 * @version 1.80
 */
@CrossOrigin
@RestController
@RequestMapping("/document")
public class DocumentController {
    @Autowired
    private DocumentService documentService;

    @PostMapping("/documentGetAll/{pageNo}/{pageSize}")
    public Result<PageInfo<Documents>> documentGetAll(@PathVariable Integer pageNo,
                                                      @PathVariable Integer pageSize,
                                                      @RequestBody(required = false) DocumentVo documentVo) {
        return Result.ok(documentService.documentGetAll(pageNo, pageSize, documentVo));
    }

    @PostMapping("/documentAdd")
    public Result<Map<String, String>> documentAdd(@RequestBody Documents documents) {
        int num = documentService.documentAdd(documents);
        Map<String, String> map = new HashMap<>();
        if (num > 0) {
            map.put("result", "true");
        } else {
            map.put("result", "false");
        }
        return Result.ok(map);
    }

    @DeleteMapping("/documentDelete/{id}/{userID}")
    public Result<Map<String, String>> documentDelete(@PathVariable Integer id, @PathVariable Integer userID) {
        Documentrecyclebin documentrecyclebin = new Documentrecyclebin();
        documentrecyclebin.setDeletedBy(userID);
        documentrecyclebin.setDocumentID(id);
        int i = documentService.documentrecyclebinAdd(documentrecyclebin);
        Map<String, String> map = new HashMap<>();
        if (i > 0) {
            int num = documentService.documentDelete(id);
            if (num > 0) {
                map.put("result", "true");
            }
        } else {
            map.put("result", "false");
        }
        return Result.ok(map);
    }

    @GetMapping("/documentGetId/{uid}")
    public Result<Documents> getDocumentId(@PathVariable Integer uid) {
        return Result.ok(documentService.documentGetId(uid));
    }

    @PutMapping("/documentUpdate")
    public Result<Map<String, String>> documentUpdate(@RequestBody Documents documents) {
        int num = documentService.documentUpdate(documents);
        Map<String, String> map = new HashMap<>();
        if (num > 0) {
            map.put("result", "true");
        } else {
            map.put("result", "false");
        }
        return Result.ok(map);
    }

}
